const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectd:false,//下载PDF
    maskorder:false, //排序
    shulists:[
      {
        'id':1,
        'library_name':"时间",
        'flage':false
      },
      {
        'id':2,
        'library_name':"书籍->章节",
        'flage':true
      }
    ],
    shuidsindex:0,//shuids
    mask:false,
    array:['全部笔记','什么笔记','毛特笔记'],
    rangindex:0,//picker下标
    wordStr:'',//输入框的内容
    page:1,
    page_size:20,
    items:[],//我的笔记
    register:false,
    member_id:null,
    shulist:[],//书目列表
    shuid:[],//shuid
    kong:false,//是否为空状态
    domain: Api.domain,
    typenote:1, //本题笔记 1  我的笔记  2
    subject_id:0,//题目id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(!options.id){ //不存在
      this.setData({  //书籍id
        typenote:2
      });
    }else{
      this.setData({  //书籍id
        subject_id:options.id
      });
    }
    
    this.tilist();
    this.setData({
      member_id:wx.$cache.get('member_id')
    });
    console.log(this.data.subject_id);
  },
  /********
   * 笔记切换
   * typeid  类型id  1本题笔记   2 全部笔记
   * *********/
  tapChangpen: function(e) {
    this.setData({
      typenote:e.currentTarget.dataset.type
    })
    this.tilist();
   //console.log(this.data.typenote);
  },

  sed_kuang(e){//选中
    const {index} = e.currentTarget.dataset
    this.setData({
      [`items[${index}].flage`]:!this.data.items[index].flage
    })
    let seledNum = 0
    this.data.items.forEach(el=>{
        if(el.flage){
          seledNum++
        }
    })
    if(seledNum>0){
      this.setData({
        selectd:true
      })
    }else{
      this.setData({
        selectd:false
      })
    }

  },
  print_pdf(){
    let bijiids = [];
    for (const item of this.data.items) {
      if(item.flage == true){
        bijiids.push(item.id)
      }
    }
    console.log(bijiids);
    let params = {
      id:bijiids + ''
    }
    wx.showLoading({
      title: '下载中',
    })

    const fileExtName = ".pdf";
   const randfile = new Date().getTime() + fileExtName;
   const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
   
    wx.$http.get(wx.$get.note_my_list_down,params).then(res=>{
      console.log(res);
      wx.downloadFile({
        url: res, //仅为示例，并非真实的资源
        filePath: newPath,
        success: function (res) {
          if (res.statusCode === 200) {
            wx.hideLoading()
            wx.showToast({
              title: '下载成功',
              icon: 'success',
              duration: 2000
            }) 
            //关闭下载按钮，并隐藏
            
            const filePath = res.filePath;
            wx.openDocument({
              filePath: filePath,
              showMenu: true,
              fileType: 'pdf',
              success: function (res) {
                console.log('打开文档成功', res)
              }
            })  
          }  
        },
        fail: function (err) {
          console.log(err)
        }  
      })
    })


  },
  

  tilist(){//全部题库列表
    let params = {
      typenote:this.data.typenote,//类型id 1本题笔记   2 全部笔记
      subject_id:this.data.subject_id,//题目id
      page:1,
      page_size:100,
      member_id:wx.$cache.get('member_id')
    }
    wx.$http.get(wx.$get.library_all_list,params).then(res=>{
      console.log(res,'全部题库')
      let shuid = []
      for(let i=0;i<res.length;i++){
        shuid = shuid.concat(res[i].id)
      }
      this.setData({
        shulist:res,
        shuid
      })
      console.log(this.data.shuid,'获取输入的id');
      this.mybi()
      this.mybiid()
    })
  },
  mybi(){//我的笔记
    this.setData({
      page:1
    })
    let params = {
      typenote:this.data.typenote,//类型id 1本题笔记   2 全部笔记
      subject_id:this.data.subject_id,//题目id
      page:this.data.page,
      page_size:this.data.page_size,
      content:this.data.wordStr,
      library_ids:this.data.shuid.toString(),
      ordertype:this.data.shulists[this.data.shuidsindex].id
    }
    wx.$http.get(wx.$get.note_my_list,params).then(res=>{
      console.log(res,'我的笔记')
      let ids = []      
      app.ids = ids
      res.forEach(item => {
        app.ids = app.ids.concat(item.id)
      })
      if(res.length==0){
        this.setData({
          kong:true
        })
      }else{
        this.setData({
          kong:false
        })
      }
      this.setData({
        items:res
      })
    })
  },
  mybiid(){//我的笔记id
    let params = {
      typenote:this.data.typenote,//类型id 1本题笔记   2 全部笔记
      subject_id:this.data.subject_id,//题目id
      page:this.data.page,
      page_size:200,
      content:this.data.wordStr,
      library_ids:this.data.shuid.toString(),
      ordertype:this.data.shulists[this.data.shuidsindex].id
    }
    wx.$http.get(wx.$get.note_my_id,params).then(res=>{
      console.log(res,'我的笔记id')
      // app.ids = res
    })
  },
  mask(){
    this.setData({
      mask:true
    })
  },
  noMask(){
    this.setData({
      mask:false,
    })
  },

  maskorder(){
    this.setData({
      maskorder:true
    })
  },
  noMaskorder(){
    this.setData({
      maskorder:false,
    })
  },

  godetail(e){
    const {id} = e.currentTarget.dataset
    console.log(id)
    wx.navigateTo({
      url: '/pages/noteDetails/noteDetails?id='+id,
    })
  },
  wordStr(e){
    this.setData({
      wordStr:e.detail.value
    })
  },
  serch(){//搜索
    this.mybi()
    this.mybiid()
  },

  check(e){//书目筛选
    const {index} = e.currentTarget.dataset
    const {flage} = e.currentTarget.dataset
    const {id} = e.currentTarget.dataset
    let {shuid} = this.data
    // arr= arr.filter((x) => x !== '待删除元素');
    this.setData({
      [`shulist[${index}].flage`]:!this.data.shulist[index].flage
    })
    if(flage){
      shuid = shuid.concat(id)
      this.setData({
        shuid
      })
      console.log(this.data.shuid,'11111111111111111111')
    }else{
      let arr= shuid.filter((x) => x !== id)
      this.setData({
        shuid:arr
      })
      console.log(this.data.shuid,'222222222222222222222222222')
    }
  },
  //排序
  checkorder(e){//排序
    let index = e.currentTarget.dataset.index;
    
    this.setData({
      shuidsindex:index
    })

    if(this.data.shulists[index].flage){
      this.setData({
        [`shulists[${index}].flage`]:false
      })
      
      for (var i = 0; i < this.data.shulists.length; i++) {
        if(index==i){
          continue;
        }
        this.setData({
          [`shulists[${i}].flage`]:true
        })
      }
    }

    
    // arr= arr.filter((x) => x !== '待删除元素');
    

    
    
  },



  sure(){//确定
    this.noMask()
    this.mybi()
    this.mybiid()
  },

  sureorder(){//确定排序
    this.noMaskorder()
    this.mybi()
    this.mybiid()
  },

  del(e){//笔记删除
    const {id} = e.currentTarget.dataset
    var that = this
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗',
      success:(res)=> {
        if (res.confirm) {
          var params = {
            note_id:id
          }
          wx.$http.get(wx.$get.note_del,params).then(res=>{
            console.log(res,'删除笔记')
            this.mybi()
            this.mybiid()
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goedit(e){//去修改笔记
    const {id} = e.currentTarget.dataset
    let {type} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/newnote/newnote?bjid='+id+'&type='+type,
    })
  },
  downpull(){//我的笔记

    console.log("3434erer34er3434er34er3434");

    this.setData({
      page:this.data.page+=1
    })
    let params = {
      typenote:this.data.typenote,//类型id 1本题笔记   2 全部笔记
      subject_id:this.data.subject_id,//题目id
      page:this.data.page,
      page_size:this.data.page_size,
      content:this.data.wordStr,
      library_ids:this.data.shuid.toString()
    }
    wx.$http.get(wx.$get.note_my_list,params).then(res=>{
      // console.log(res,'我的笔记')//
      res.forEach(item => {
        app.ids = app.ids.concat(item.id)
      })
      if(res.length!=0){
        this.setData({
          items:[...this.data.items,...res]
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.downpull()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})